package com.mj.mafeed.activity;

import com.mj.mafeed.R;
import com.mj.mafeed.bean.BookLogJSON;
import com.mj.mafeed.bean.ProductJSON;
import com.mj.mafeed.service.BookLogService;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.util.RegUtil;
import com.mj.mafeed.widget.LoadingDialog;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 商品预约
 * @author zhaominglei
 * @date 2016-3-28
 * 
 */
public class BookLogActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = BookLogActivity.class.getSimpleName();
	private BookLogService bookLogService = new BookLogService();
	private ImageView goHome;
	private TextView productNameTxt; //名称
	private TextView productPriceTxt; //价格
	private TextView productWeightTxt; //重量
	private EditText quantityEdt; //数量
	private String quantity;
	private EditText areanameEdt; //地区
	private String areaname;
	private EditText nameEdt; //姓名
	private String name;
	private EditText mobileEdt; //手机号
	private String mobile;
	private Button saveBtn; //保存
	private ProductJSON productJSON; //商品详情
	private String referer; //来自那个链接
	private LoadingDialog loadingDialog;
	private BookLogJSON bookLogJSON;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_booklog);
		init();
		
	}

	private void init() {
		goHome = (ImageView)findViewById(R.id.booklog_gohome);
		productNameTxt = (TextView)findViewById(R.id.booklog_product_name);
		productPriceTxt = (TextView)findViewById(R.id.booklog_product_price);
		productWeightTxt = (TextView)findViewById(R.id.booklog_product_weight);
		quantityEdt = (EditText)findViewById(R.id.booklog_quantity);
		areanameEdt = (EditText)findViewById(R.id.booklog_areaname);
		nameEdt = (EditText)findViewById(R.id.booklog_name);
		mobileEdt = (EditText)findViewById(R.id.booklog_mobile);
		saveBtn = (Button)findViewById(R.id.booklog_save);
		
		goHome.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		referer = getIntent().getStringExtra("referer");
		if (referer != null && referer.equals("ProductDetailActivity")) {
			productJSON = (ProductJSON)getIntent().getSerializableExtra("productJSON");
			productNameTxt.setText("名称: "+productJSON.getName());
			productPriceTxt.setText("价格: "+String.valueOf(productJSON.getPrice())+" 元");
			productWeightTxt.setText("重量: "+String.valueOf(productJSON.getWeight()));
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.booklog_gohome:
			BookLogActivity.this.finish();
			break;
		
		case R.id.booklog_save:
			book(v);
			break;
			
		default:
			break;
		}
	}
	
	private void book(View v) {
		quantity = quantityEdt.getText().toString();
		if (quantity == null || quantity.equals("")) {
			Toast.makeText(v.getContext(), R.string.booklog_quantity_hint, Toast.LENGTH_SHORT).show();
			return;
		}
		if (!RegUtil.isNumber(quantity)) {
			Toast.makeText(v.getContext(), R.string.booklog_quantity_hint2, Toast.LENGTH_SHORT).show();
			return;
		}
		areaname = areanameEdt.getText().toString();
		if (areaname == null || areaname.equals("")) {
			Toast.makeText(v.getContext(), R.string.booklog_areaname_hint, Toast.LENGTH_SHORT).show();
			return;
		}
		name = nameEdt.getText().toString();
		if (name == null || name.equals("")) {
			Toast.makeText(v.getContext(), R.string.booklog_name_hint, Toast.LENGTH_SHORT).show();
			return;
		}
		mobile = mobileEdt.getText().toString();
		if (mobile == null || mobile.equals("")) {
			Toast.makeText(v.getContext(), R.string.booklog_mobile_hint, Toast.LENGTH_SHORT).show();
			return;
		}
		if (!RegUtil.isMobile(mobile)) {
			Toast.makeText(v.getContext(), R.string.register_account_hint2, Toast.LENGTH_SHORT).show();
			return;
		}
		bookLogJSON = new BookLogJSON();
		bookLogJSON.setProductId(productJSON.getProductId());
		bookLogJSON.setQuantity(Integer.valueOf(quantity));
		bookLogJSON.setAreaName(areaname);
		bookLogJSON.setName(name);
		bookLogJSON.setMobile(mobile);
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new BookTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}		
	}

	@Override
	public void onBackPressed() {
		BookLogActivity.this.finish();
	}
	
	public class BookTask extends AsyncTask<String, String, Boolean> {
		@Override
		protected Boolean doInBackground(String... params) {
			return bookLogService.book("BookLogActivity", bookLogJSON);
		}
		@Override
		protected void onPostExecute(Boolean result) {
			if (result) {
				Toast.makeText(getApplicationContext(), R.string.booklog_success, Toast.LENGTH_SHORT).show();
				BookLogActivity.this.finish();
			} else {
				Toast.makeText(getApplicationContext(), R.string.booklog_error, Toast.LENGTH_SHORT).show();
			}
		}
	}
}
